package com.cy.common.aspect;

import org.apache.ibatis.cache.Cache;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Aspect
public class SysCatchAspect {
    private Map<Object,Object> cache=new ConcurrentHashMap<>();
    //假设此为chche
    @Pointcut("@annotation(com.cy.common.annotation.RequiredCache)")
    public void doCatch(){}
    @Pointcut("@annotation(com.cy.common.annotation.CleanCache)")
    public void doCleanCache(){}
    @AfterReturning("doCleanCache()")
    public void doadd(JoinPoint jp){
            cache.clear();
    }
    @Around("doCatch()")
    public Object around(ProceedingJoinPoint pj)

        throws Throwable{

        Object cachee1=cache.get("dept");
        Object cachee2=cache.get("dept2");
        System.out.println("Get data from cache");
        if(cachee1!=null) {

            return cachee1;
        }

        cachee1=pj.proceed();
        System.out.println("Put data from cache");
        cache.put("dept",cachee1);
        return cachee1;
    }




}
